package com.kczd.jinlan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.activity.FoodDetailsActivity;
import com.kczd.jinlan.adapter.MenuDialogAdapter;
import com.kczd.jinlan.adapter.MyPagerAdapter;
import com.kczd.jinlan.model.MenuData;
import com.kczd.jinlan.myview.MyViewPager;
import com.kczd.jinlan.utils.MenuDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * yang_g
 * 分类模块
 * Created by Administrator on 2017/5/15.
 */

public class ClassificationFragment extends Fragment {

    public MenuDataManager menuDataManager = MenuDataManager.getInstance();
    private MyViewPager mViewPager;
    private View view1,view2,view3;
    private ListView mListView1,mListView2,mListView3;
    private MenuDialogAdapter mListView1Adapter, mListView2Adapter, mListView3Adapter;
    private List<View> views = new ArrayList<View>();
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.search_class_listviews,null);
        initViews();
        return view;
    }

    //操作控件
    private void initViews() {
        //一级
        mViewPager = (MyViewPager) view.findViewById(R.id.viewpager);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        view1 = inflater.inflate(R.layout.pager_number, null);
        view2 = inflater.inflate(R.layout.pager_number, null);
        view3 = inflater.inflate(R.layout.pager_number, null);
        mListView1 = (ListView) view1.findViewById(R.id.listview);
        mListView2 = (ListView) view2.findViewById(R.id.listview);
        mListView3 = (ListView) view3.findViewById(R.id.listview);

        List<MenuData> list1=menuDataManager.getTripleColumnData(getActivity(), 0);
        mListView1Adapter = new MenuDialogAdapter(getActivity(), list1);
        mListView1Adapter.setSelectedBackgroundResource(R.drawable.select_white);//选中时
        mListView1Adapter.setHasDivider(false);
        mListView1Adapter.setNormalBackgroundResource(R.color.menudialog_bg_gray);//未选中
        mListView1.setAdapter(mListView1Adapter);

        views.add(view1);
        views.add(view2);//加载了一二级菜单
        mViewPager.setAdapter(new MyPagerAdapter(views));

        mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListView1Adapter != null)
                    mListView1Adapter.setSelectedPos(position);
                if (mListView2Adapter != null)
                    mListView2Adapter.setSelectedPos(-1);

                if (views.contains(view3)) {
                    views.remove(view3);
                    mViewPager.getAdapter().notifyDataSetChanged();
                }
                MenuData menuData = (MenuData) parent.getItemAtPosition(position);
                if (menuData.id == 0) {//不限
                    if (mListView2Adapter != null) {
                        mListView2Adapter.setData(new ArrayList<MenuData>());
                        mListView2Adapter.notifyDataSetChanged();
                    }

                    setResultDate(menuData);
                } else {
                    List<MenuData> list2 = menuDataManager.getTripleColumnData(getActivity(), menuData.id);
                    if (mListView2Adapter == null) {
                        mListView2Adapter = new MenuDialogAdapter(getActivity(), list2);
                        mListView2Adapter.setNormalBackgroundResource(R.color.white);
                        mListView2.setAdapter(mListView2Adapter);
                    } else {
                        mListView2Adapter.setData(list2);
                        mListView2Adapter.notifyDataSetChanged();
                    }
//                    mRootView.invalidate();
                }
            }
        });

        //二级
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                if (mListView2Adapter != null) {
                    mListView2Adapter.setSelectedPos(position);
                    mListView2Adapter.setSelectedBackgroundResource(R.drawable.select_gray);//选中时
                }

                if (views.contains(view3)) {
                    views.remove(view3);
                }

                MenuData dictUnit = (MenuData) parent.getItemAtPosition(position);
                List<MenuData> list3 = menuDataManager.getTripleColumnData(getActivity(), dictUnit.id);
                if (mListView3Adapter == null) {
                    mListView3Adapter = new MenuDialogAdapter(getActivity(), list3);
                    mListView3Adapter.setHasDivider(false);
                    mListView3Adapter.setNormalBackgroundResource(R.color.menudialog_bg_gray);//未选中
                    mListView3.setAdapter(mListView3Adapter);
                } else {
                    mListView3Adapter.setData(list3);
                    mListView3Adapter.notifyDataSetChanged();
                }

                views.add(view3);
                mViewPager.getAdapter().notifyDataSetChanged();
                mViewPager.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mViewPager.setCurrentItem(views.size()-1);
                    }
                }, 300);
            }
        });
        mListView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuData menuData = (MenuData) parent.getItemAtPosition(position);
                setResultDate(menuData);
            }
        });

    }

    //传递值
    private void setResultDate(MenuData menuData){
//        Intent intent=new Intent();
//        intent.putExtra("menu",(Serializable)menuData);
//        setResult(0, intent);
//        finish();

        Intent intent=new Intent(getActivity(), FoodDetailsActivity.class);
        intent.putExtra("name",menuData.name);
        startActivity(intent);
//        Toast.makeText(getActivity(),"蔬菜叫做"+menuData.name+"它的ID是"+menuData.id,Toast.LENGTH_SHORT).show();
    }
}
